/**
 * 用户数据模型 
 * @author LuoDong
 * @email luo4521@online.net.tw
 */
package com.addcn.texas.model
{		
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;	
	
	import com.addcn.common.event.DataEvent;
	import com.addcn.common.utils.GameLoader;
	import it.gotoandplay.smartfoxserver.SmartFoxClient;
	import it.gotoandplay.smartfoxserver.SFSEvent;
	
	import com.addcn.fog.model.Model;	
	import com.addcn.common.utils.Server;	
	import com.addcn.common.data.UserInfoData;
	import com.addcn.common.event.ParamEvent;
	import com.addcn.texas.utils.GetPhoto;		
	import com.addcn.texas.utils.PicBitMap;		
	import com.addcn.texas.utils.DataResolve;
	import com.addcn.texas.data.MyUser;
	import com.addcn.texas.data.User;
	
	public class UserInfoModel extends Model 
	{
		public static const USER_INFO_ERR : String = "userInfoError";
		public static const USER_LOGIN_SUCC : String = "useLoginSucc";		
		public static const USER_INFO_MODIFY : String = "userInfoModify";
		
		public static const UPDATE_USER_INFO : String = "updateUserInfo";
		
		private var _server : SmartFoxClient = Server.getInstance();	
		
		public function UserInfoModel() : void {					
			_server.addEventListener(SFSEvent.onExtensionResponse, _responseHandler);	
			if (MyUser.getInstance().id) {			
				updataUserInfo();				
			}
		}
		
		//服務端回調
		private function _responseHandler(evt : SFSEvent) : void
		{
			
			var _data = evt.params.dataObj.dataObj;						
			switch(evt.params.dataObj._cmd) 
			{
				case "loginOk":
					if (_data["err"] ) {
						sendNotification(USER_INFO_ERR, { code:String(_data.err)} );
					}else {	
						var _resslve : DataResolve = DataResolve.getInstance();  
						var _info : Object = _resslve._resolveUser(_data.user);	
						MyUser.getInstance().initialize(_info);		
						updataUserInfo();
						_loadPhoto();
					}
					break;			
				case "upGold":					
					var _gold : uint = MyUser.getInstance().gold = _data["gold"];
					sendNotification(UPDATE_USER_INFO, { gold:_gold } );					
					break;
			}
		}		
					
		/**
		 * 发送玩家信息通知
		 */
		private function updataUserInfo() : void
		{
			var _user : MyUser = MyUser.getInstance();
			sendNotification (UPDATE_USER_INFO, _user.getUserInfo());
		}
		
		
		private function _loadPhoto() : void
		{					
			var _url : String = MyUser.getInstance().photo;			
			if (_url) {				
				var _loader : GameLoader = new GameLoader(ApplicationDomain.currentDomain);
				_loader.addEventListener(GameLoader.COMPLETE, _loadCompleteHalder);
				//_loader.addEventListener(GameLoader.IO_ERROR, _loadErrHandler);
				//_loader.addEventListener(GameLoader.TIME_OUT, _loadErrHandler);
				_loader.add(_url, { id:"photo", name : MyUser.getInstance().id } );
				_loader.start();				
			}			
		}
		
		private function _loadCompleteHalder(evt : DataEvent) : void {			
			var _content = evt.target.getFile("photo");
			var _uid : int = evt.data["name"];
			MyUser.getInstance().pic = _content;				
			sendNotification(UPDATE_USER_INFO, { uid:_uid, "pic": _content} );	
		}
						
		override public function destroy():void 
		{
			_server.removeEventListener(SFSEvent.onExtensionResponse, _responseHandler);
		}
		
	}
	
}
